/**
 * @description: 转盘抽奖组件
 * @update: 2019-05-30
 * @version: 1.0.0
 **/
(function ($) {
    $.fn.WheelSurf = function (options) {
        var _default = {
            width: 500,
            height: 500,
            outerCircle: "",
            innerCircle: "",
            dots: "",
        };
        $.extend(_default, options);
        var _this = this
            , width = _default.width
            , height = _default.height
            , url_base = "https://www.changyou.com/cdn/turntable/2.0.0/img/";
        $(this).css({
            'backgroundColor': '#102129',
            'width': width,
            'height': height,
            'position': 'relative',
            'margin': '0 auto'
        });
        return {
            init: function () {
                var $wrapper = $('<div class="turntbale_wrapper">').css({
                    'background': 'url('+ url_base +'circle.png)',
                    'width': width,
                    'height': height,
                    'backgroundSize': '100%'
                }).appendTo($(_this));
                $('<img class="img_light">').attr('src', url_base + 'light.png').css({
                    'position': 'absolute',
                    'left': 0,
                    'right': 0,
                    'width': width,
                    'height': height
                }).appendTo($wrapper);
                $('<img class="img_inner">').attr('src', url_base + 'inner.png').css({
                    'position': 'absolute',
                    'width': width,
                    'height': height
                }).appendTo($wrapper);
                $('<img class="point">').attr('src', url_base + 'point.png').css({
                    'width': width * 0.37,
                    'height': height * 0.4,
                    'position': 'absolute',
                    'left': 0,
                    'right': 0,
                    'top': width * 0.31,
                    'margin': '0 auto'
                }).appendTo($(_this));

                var num = 1;
                var prize_arr = {
                    "1": 206,   //谢谢参与
                    "2": 51,    //50元话费卡
                    "3": 255,   //100元话费卡
                    "4": 100,   //100元京东卡
                    "5": 152,   //小米随身音响
                    "7": 305,   //华为Mate20Pro
                    "8": 360    //IPhoneXsmax
                };
                $(".point").on('click', function () {
                    var prizeCode = 1;
                    var argle = num * 720 + prize_arr[prizeCode];
                    $('.turntbale_wrapper').css({
                        WebkitTransform: 'rotate('+argle+'deg)',
                        transition: '2s'
                    });
                    num ++;
                });
            },
            /**
             * angel 旋转角度
             * callback 转完后的回调函数
             */
            lottery: function (angel, callback) {
                angel = angel || 0;
                angel = 360 - angel;
                angel += 720;
                // 基值（减速）
                var baseStep = 30;
                // 起始滚动速度
                var baseSpeed = 0.3;
                // 步长
                var count = 1;
                var _this = this;
                var timer = setInterval(function () {
                    _this.init(count);
                    if (count == angel) {
                        clearInterval(timer);
                        if (typeof callback == "function") {
                            callback()
                        }
                    }
                    count = count + baseStep * (((angel - count) / angel) > baseSpeed ? baseSpeed : ((angel - count) / angel));
                    if (angel - count < 0.5) {
                        count = angel
                    }
                }, 25)
            }
        }
    }
}(jQuery));